#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(s) s.begin(), s.end()
const int MOD = 998244353;

void solve() {
	int n; cin >> n;
	vector<int> a(n + 1);
	map<int, int> mp;
	vector<int> v; v.push_back(0);
	int m = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (!mp[a[i]]) {
			mp[a[i]] = ++m;
			v.push_back(a[i]);
		}
		a[i] = mp[a[i]];
	}
	vector<ll> dp(m + 1, -1e15);
	ll d = 0, mx = -1e15;
	dp[0] = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] == a[i - 1]) {
			d += v[a[i]];
		} else dp[a[i - 1]] = max({0ll, dp[a[i]] + v[a[i]], mx});
		mx = max(mx, dp[a[i - 1]]);
	}
	cout << *max_element(all(dp)) + d << '\n';
	return;
}

int main() {
	auto start = chrono::steady_clock::now();
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	auto end = chrono::steady_clock::now();
	cerr << "time: " << chrono::duration<double, milli>(end - start).count() << "ms" << endl;
}